package com.java.example.jpush

import android.content.Context
import android.text.TextUtils
import cn.jpush.android.api.CustomMessage
import cn.jpush.android.api.JPushMessage
import cn.jpush.android.api.NotificationMessage
import cn.jpush.android.service.JPushMessageReceiver
import com.alibaba.fastjson2.JSON
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.java.example.app.AccountManager.currentUser
import com.java.example.app.ActivityManager.Companion.instance
import com.java.example.bean.EventType
import com.java.example.bean.JPushMessageTaskBean
import com.java.example.bean.MessageEvent
import org.greenrobot.eventbus.EventBus

class MyJpushReceiver : JPushMessageReceiver() {
    override fun onTagOperatorResult(context: Context, jPushMessage: JPushMessage) {
        TagAliasOperatorHelper.instance!!.onTagOperatorResult(context, jPushMessage)
        super.onTagOperatorResult(context, jPushMessage)
    }

    override fun onCheckTagOperatorResult(context: Context, jPushMessage: JPushMessage) {
        TagAliasOperatorHelper.instance!!.onCheckTagOperatorResult(context, jPushMessage)
        super.onCheckTagOperatorResult(context, jPushMessage)
    }

    override fun onAliasOperatorResult(context: Context, jPushMessage: JPushMessage) {
        TagAliasOperatorHelper.instance!!.onAliasOperatorResult(context, jPushMessage)
        super.onAliasOperatorResult(context, jPushMessage)
    }

    override fun onMobileNumberOperatorResult(context: Context, jPushMessage: JPushMessage) {
        TagAliasOperatorHelper.instance!!.onMobileNumberOperatorResult(context, jPushMessage)
        super.onMobileNumberOperatorResult(context, jPushMessage)
    }

    override fun onMessage(context: Context, customMessage: CustomMessage) {
        super.onMessage(context, customMessage)
    }

    override fun onNotifyMessageArrived(context: Context, notificationMessage: NotificationMessage) {
        super.onNotifyMessageArrived(context, notificationMessage)
        try {
//			Logger.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
            val activity = instance!!.topActivity
            val activityName = activity!!.javaClass.simpleName
            val extra = notificationMessage.notificationExtras
            val type = JSON.parseObject(extra).getString("type")
            val `object` = Any()
            LogUtils.i("extra ", extra)
            //如果在当前页面，刷新
            if (!TextUtils.isEmpty(type)) {
                when (type) {
                    "pick_up_good_order" -> {
                        val id = JSON.parseObject(extra).getString("project_id")
                        if (!TextUtils.isEmpty(id)) {
                            SPUtils.getInstance().put("key_id", id)
                        }
                    }

                    "cave_note_alert" -> EventBus.getDefault().post(MessageEvent(EventType.UPDATE_HOME3, null))
                    "project_delete" -> {
                        //更新
                        val taskBean = JSON.parseObject(extra, JPushMessageTaskBean::class.java)
                        val mUser = currentUser ?: return
                        if (taskBean != null) {
                            val workerId = taskBean.project_worker_id
                            if (activityName.contains("MainActivity")) {
                                val stageId = taskBean.project_stage_id
                                //                                LatteDelegate delegate = (LatteDelegate) ((MainActivity) activity).getTopFragment();
//                                if (delegate instanceof EcBottomDelegate) {
//                                    int index = ((EcBottomDelegate) delegate).getCurrentDelegateIndex();
//                                    if (index == 0) {
//                                        if (stageId >= 1 && stageId <= 5) {
//                                            EventBus.getDefault().post(new MessageEvent(EventType.UPDATE_HOME3, null));
//                                        }
//                                        //更新首页，项目管理，首页子类
//                                        //发送消息更新
//                                    } else if (index == 1) {
//                                        //发送消息，判断更新
//                                        //直接发送消息，将项目状态也发送过去
//                                        EventBus.getDefault().post(new MessageEvent(EventType.UPDATE_PROJECT_MANAGER, stageId));
//                                    }
//                                }
                            }
                        }
                    }

                    "project_participant" -> {
                        //更新
                        val bean1 = JSON.parseObject(extra, JPushMessageTaskBean::class.java)
                        val mUser1 = currentUser ?: return
                        if (bean1 != null) {
                            if (activityName.contains("MainActivity")) {
//                                int stageId = bean1.getProject_stage_id();
//                                LatteDelegate delegate = (LatteDelegate) ((MainActivity) activity).getTopFragment();
//                                if (delegate instanceof EcBottomDelegate) {
//                                    int index = ((EcBottomDelegate) delegate).getCurrentDelegateIndex();
//                                    if (index == 1) {
//                                        EventBus.getDefault().post(new MessageEvent(EventType.UPDATE_PROJECT_MANAGER, stageId));
//                                    }
//                                }
                            }
                        }
                    }

                    "project_stage", "project_add_participant", "project_change_manager", "project_completion_acceptance", "close_project", "delete_project", "project_delete_participant", "reopen_project", "project_add_expenditure", "urge_project", "account_upgrade", "account_about_expire", "account_expired", "reply_suggestion", "add_friend", "measure_about_expire", "measure_expired", "design_about_expire", "design_expired", "project_set_picker", "project_change_picker", "project_completion_pick", "pick_up_good_order_about_expire", "pick_up_good_order_expired", "install_about_expire", "install_expired", "after_sale_set_service", "after_sale_change_service", "after_sale_complete", "after_sale_about_expire", "after_sale_expired", "project_reset_surveyor", "project_reset_designer", "project_reset_picker", "project_reset_installer", "after_sale_reset_service" -> {
                        val bean2 = JSON.parseObject(extra, JPushMessageTaskBean::class.java)
                        val mUser2 = currentUser ?: return
                        val stageId = bean2!!.project_stage_id
                        val workerId = bean2.project_worker_id
                        if (bean2 != null) {

                        }
                    }

                    "friends" -> {}
                    "measure_wait_start", "measure_start_soon", "measure_complete", "design_wait_start", "design_start_soon", "design_complete", "install_wait_start", "install_start_soon", "install_complete", "install_exception", "pick_up_good_exception" -> {
                        EventBus.getDefault().post(MessageEvent(EventType.HOME_LOOP_MESSAGE_UPDATE, null))
                        EventBus.getDefault().post(MessageEvent(EventType.HOME_VOICE_OPEN, null))
                    }

                    "mccc_order_status" -> {}
                }
            }
            //				Logger.d(TAG, "[MyReceiver] 接收到推送下来的通知");
//				Log.i("AAAA","接收到推送下来的通知");
//				int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
//				String tex=bundle.getString(JPushInterface.EXTRA_EXTRA);
//				Log.i("AAAA","content2:"+tex);
//				{"account_id":"323d9a70-22c1-11ea-944b-07f9dab05943"}
        } catch (e: Exception) {
        }
    }
}
